package cn.weblog.api.controller;

import cn.weblog.api.domain.Passage;
import cn.weblog.api.domain.User;
import cn.weblog.api.service.CatagoryService;
import cn.weblog.api.service.KeywordService;
import cn.weblog.api.service.PassageService;
import cn.weblog.api.service.UserService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/passage")
public class PassageController {

	@Autowired
	private PassageService passageService;

	@Autowired
	private CatagoryService catagoryService;

	@Autowired
	private KeywordService keywordService;

	@Autowired
	private UserService userService;

/*	@RequestMapping("/isDraft/{id}/{status}")
	@ResponseBody
	public String isDraft(@PathVariable Integer id,@PathVariable Integer status){

	}*/

	@RequestMapping("/findPassage/{id}")	//t 前台页面查询文章详情
	@ResponseBody
	public String findPassage(@PathVariable Integer id){
		Passage passage = passageService.findById(id);
		Map<String,Object> result=new HashMap<>();
		result.put("passage",passage);
		User user = userService.findUserMore(passage.getUserId());
		result.put("currentUser",user);
		System.out.println(JSON.toJSONString(result));
		return JSON.toJSONString(result);
	}

	@ResponseBody
	@GetMapping("/findPassages")		//t 首页显示数据查询
	public String findByCreateDate(){
		Map<String,Object> result=new HashMap<>();
		// 查找首页文章
		result.put("passages",passageService.findByCreateDate());
		// 查找前十的关键词
		result.put("keywords",keywordService.findTop10());
		return JSON.toJSONString(result);
	}

	@RequestMapping("/findById/{id}")	//t 后台管理页面查询某一篇特定文章
	@ResponseBody
	public String findByPassageId(@PathVariable Integer id){
		Map<String,Object> result=new HashMap<>();
		result.put("passage", passageService.findById(id));
		return JSON.toJSONString(result);
	}

	@GetMapping("/findByCatagory/{userId}/{id}")	//t 查询用户分类
	@ResponseBody
	public String findByCatagory(@PathVariable Integer id,@PathVariable Integer userId) {
		List<Passage> passages = passageService.findByCatagoryAndUser(userId, id);
		Map<String,Object> result=new HashMap<>();
		if(passages.size()==0){
			result.put("error","该分类下没有文章");
			result.put("passageList",passages);
			return JSON.toJSONString(result);
		}
		result.put("catagoryName",passages.get(0).getCatagoryName());
		result.put("passageList",passages);
		return JSON.toJSONString(result);
	}

	@GetMapping("/findByCollectCatagory/{userId}/{id}")	//t 查询收藏分类
	@ResponseBody
	public String findByCollectCatagory(@PathVariable Integer id,@PathVariable Integer userId) {
		List<Passage> passages = passageService.findByCatagoryAndUser(userId, id);
		Map<String,Object> result=new HashMap<>();
		if(passages.size()==0){
			result.put("passageList",passages);
			return JSON.toJSONString(result);
		}
		result.put("catagoryName",passages.get(0).getCatagoryName());
		result.put("passageList",passages);
		return JSON.toJSONString(result);
	}

	@RequestMapping("/findCollectPassage/{userId}")	//t 查询收藏文章列表
	@ResponseBody
	public String findCollectPassage(@PathVariable Integer userId) {
		Map<String,Object> result=new HashMap<>();
		result.put("passageList",
				passageService.findCollectPassage(userId));
		return JSON.toJSONString(result);
	}

	@RequestMapping("/findCatagory/{userId}")		//t 查询用户分类
	@ResponseBody
	public String writeBlog(@PathVariable Integer userId){
		Map<String,Object> result=new HashMap<>();
		result.put("catagoryList",
				catagoryService.findByUserId(userId));
		return JSON.toJSONString(result);
	}

	@RequestMapping("/add/{userId}")	//t 写文章 post请求
	@ResponseBody
	public String add(@RequestBody Passage passage,@PathVariable Integer userId){
		passage.setUserId(userId);
		passage.setCreateDate(LocalDateTime.now());
		int i = passageService.addPassage(passage);
	//	Map<String,Object> result=new HashMap<>();
		String msg;
		if(i==1){
			msg="添加成功";
		}else{
			msg="添加失败";
		}
		return msg;
	}

	@RequestMapping("/findDraft/{userId}")//t 查询草稿箱
	@ResponseBody
	public String findDraft(@PathVariable Integer userId){
		Map<String,Object> result=new HashMap<>();
		result.put("passageList",
				passageService.findDraft(userId));
		return JSON.toJSONString(result);
	}

	@RequestMapping("/findDelete/{userId}")//t
	@ResponseBody
	public String findDelete(@PathVariable Integer userId){
		Map<String,Object> result=new HashMap<>();
		result.put("passageList",
				passageService.findDelete(userId));
		return JSON.toJSONString(result);
	}

	@RequestMapping("/findByUserId/{userId}")//t 查询用户文章列表
	@ResponseBody
	public String findByUserId(@PathVariable Integer userId){
		Map<String,Object> result=new HashMap<>();
		result.put("passageList",
				passageService.findByUserId(userId));
		return JSON.toJSONString(result);
	}


}
